/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TextEditorPanel.java
 *
 * Created on 09/11/2009, 13:53:48
 */

package com.power.visual;

import com.power.utils.PowerImage;
import java.io.File;
import java.io.FileOutputStream;
import javax.swing.JFrame;
import javax.swing.text.AttributeSet;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import javax.swing.text.rtf.RTFEditorKit;



/**
 *
 * @author f6036477
 */
public class TextEditorPanel extends javax.swing.JPanel {


  private StyledDocument doc;

  private boolean bold, italic, underline;

  private static final String path = "T:/java/com/power/images/";

    /** Creates new form TextEditorPanel */
    public TextEditorPanel() {
        initComponents();
        doc = editor.getStyledDocument();
        bold = italic = underline = false;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    buttonsPanel = new javax.swing.JPanel();
    italicButton = new com.power.visual.BlackButton();
    boldButton = new com.power.visual.BlackButton();
    underlineButton = new com.power.visual.BlackButton();
    fontButton = new com.power.visual.BlackButton();
    centerButton = new com.power.visual.BlackButton();
    leftButton = new com.power.visual.BlackButton();
    rightButton = new com.power.visual.BlackButton();
    justifyButton = new com.power.visual.BlackButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    editor = new javax.swing.JTextPane();

    setMinimumSize(new java.awt.Dimension(400, 300));

    buttonsPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
    buttonsPanel.setPreferredSize(new java.awt.Dimension(400, 50));

    italicButton.setImageIcon(
      new PowerImage(path+ "italic-[20].png"));
    italicButton.setOverImageIcon(
      new PowerImage(path+ "italic-OVER-[20].png"));
    /*
    italicButton.setText("I");
    */
    italicButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        italicButtonActionPerformed(evt);
      }
    });

    boldButton.setImageIcon(
      new PowerImage(path+ "bold-[20].png"));
    boldButton.setOverImageIcon(
      new PowerImage(path+ "bold-OVER-[20].png"));
    /*
    boldButton.setText("B");
    */
    boldButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        boldButtonActionPerformed(evt);
      }
    });

    underlineButton.setImageIcon(
      new PowerImage(path+ "underline-[20].png"));
    underlineButton.setOverImageIcon(
      new PowerImage(path+ "underline-OVER-[20].png"));
    /*
    underlineButton.setText("U");
    */
    underlineButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        underlineButtonActionPerformed(evt);
      }
    });

    fontButton.setImageIcon(
      new PowerImage(path+ "font-[20].png"));
    fontButton.setOverImageIcon(
      new PowerImage(path+ "font-OVER-[20].png"));
    /*
    fontButton.setText("T");
    */
    fontButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        fontButtonActionPerformed(evt);
      }
    });

    centerButton.setImageIcon(
      new PowerImage(path+ "center_alignment-[20].png"));
    centerButton.setOverImageIcon(
      new PowerImage(path+ "center_alignment-OVER-[20].png"));
    /*
    centerButton.setText("C");
    */
    centerButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        centerButtonActionPerformed(evt);
      }
    });

    leftButton.setImageIcon(
      new PowerImage(path+ "left_alignment-[20].png"));
    leftButton.setOverImageIcon(
      new PowerImage(path+ "left_alignment-OVER-[20].png"));
    /*
    leftButton.setText("L");
    */
    leftButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        leftButtonActionPerformed(evt);
      }
    });

    rightButton.setImageIcon(
      new PowerImage(path+ "right_alignment-[20].png"));
    rightButton.setOverImageIcon(
      new PowerImage(path+ "right_alignment-OVER-[20].png"));
    /*
    rightButton.setText("R");
    */
    rightButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        rightButtonActionPerformed(evt);
      }
    });

    justifyButton.setImageIcon(
      new PowerImage(path+ "justify_alignment-[20].png"));
    justifyButton.setOverImageIcon(
      new PowerImage(path+ "justify_alignment-OVER-[20].png"));
    /*
    justifyButton.setText("J");
    */
    justifyButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        justifyButtonActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout buttonsPanelLayout = new javax.swing.GroupLayout(buttonsPanel);
    buttonsPanel.setLayout(buttonsPanelLayout);
    buttonsPanelLayout.setHorizontalGroup(
      buttonsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(buttonsPanelLayout.createSequentialGroup()
        .addContainerGap()
        .addComponent(fontButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addComponent(italicButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addComponent(boldButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addGap(10, 10, 10)
        .addComponent(underlineButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addComponent(leftButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addComponent(centerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addComponent(rightButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addComponent(justifyButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(19, Short.MAX_VALUE))
    );
    buttonsPanelLayout.setVerticalGroup(
      buttonsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(buttonsPanelLayout.createSequentialGroup()
        .addContainerGap()
        .addGroup(buttonsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(italicButton, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
          .addComponent(fontButton, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
          .addComponent(boldButton, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
          .addGroup(buttonsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
            .addComponent(underlineButton, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
            .addComponent(leftButton, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(centerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(rightButton, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(justifyButton, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)))
        .addContainerGap())
    );

    editor.setBorder(null);
    jScrollPane1.setViewportView(editor);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
          .addComponent(buttonsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 423, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addGap(48, 48, 48)
        .addComponent(buttonsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 48, Short.MAX_VALUE)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap())
    );
  }// </editor-fold>//GEN-END:initComponents

    private void italicButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_italicButtonActionPerformed
    {//GEN-HEADEREND:event_italicButtonActionPerformed
      MutableAttributeSet style = this.createStyle();
      StyleConstants.setItalic(style,
          !StyleConstants.isItalic(style));
      editor.setCharacterAttributes(style, false);
      editor.requestFocus();
    }//GEN-LAST:event_italicButtonActionPerformed

    private void boldButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_boldButtonActionPerformed
    {//GEN-HEADEREND:event_boldButtonActionPerformed
      MutableAttributeSet style = this.createStyle();
      System.out.println("isBold: "+ StyleConstants.isBold(style));
      StyleConstants.setBold(style,
          !StyleConstants.isBold(style));
      editor.setCharacterAttributes(style, false);
      editor.requestFocus();
    }//GEN-LAST:event_boldButtonActionPerformed

    private void underlineButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_underlineButtonActionPerformed
    {//GEN-HEADEREND:event_underlineButtonActionPerformed
      MutableAttributeSet style = this.createStyle();
      StyleConstants.setUnderline(style,
          !StyleConstants.isUnderline(style));
      editor.setCharacterAttributes(style, false);
      editor.requestFocus();
    }//GEN-LAST:event_underlineButtonActionPerformed

    private void fontButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_fontButtonActionPerformed
    {//GEN-HEADEREND:event_fontButtonActionPerformed
      FontColor fc = FontChooser.showFontChooser(this);

      if(fc == null) return;

      MutableAttributeSet style = this.createStyle();

      StyleConstants.setForeground(style, fc.getColor());
      StyleConstants.setFontFamily(style, fc.getFont().getFamily());
      StyleConstants.setFontSize(style, fc.getFont().getSize());

      editor.setCharacterAttributes(style, false);
      editor.requestFocus();
    }//GEN-LAST:event_fontButtonActionPerformed

    private void centerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_centerButtonActionPerformed
      MutableAttributeSet style = this.createStyle();
      StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);
      editor.setParagraphAttributes(style, false);
      editor.requestFocus();
    }//GEN-LAST:event_centerButtonActionPerformed

    private void leftButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_leftButtonActionPerformed
    {//GEN-HEADEREND:event_leftButtonActionPerformed
      MutableAttributeSet style = this.createStyle();
      StyleConstants.setAlignment(style, StyleConstants.ALIGN_LEFT);
      editor.setParagraphAttributes(style, false);
      editor.requestFocus();
    }//GEN-LAST:event_leftButtonActionPerformed

    private void rightButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_rightButtonActionPerformed
    {//GEN-HEADEREND:event_rightButtonActionPerformed
      MutableAttributeSet style = this.createStyle();
      StyleConstants.setAlignment(style, StyleConstants.ALIGN_RIGHT);
      editor.setParagraphAttributes(style, false);
      editor.requestFocus();
    }//GEN-LAST:event_rightButtonActionPerformed

    private void justifyButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_justifyButtonActionPerformed
    {//GEN-HEADEREND:event_justifyButtonActionPerformed
      MutableAttributeSet style = this.createStyle();
      StyleConstants.setAlignment(style, StyleConstants.ALIGN_JUSTIFIED);
      editor.setParagraphAttributes(style, false);
      editor.requestFocus();
    }//GEN-LAST:event_justifyButtonActionPerformed


    protected MutableAttributeSet createStyle()
    {
        AttributeSet a;

        if(editor.getSelectionStart() != editor.getSelectionEnd()) {
          int pos = editor.getSelectionStart();
          int end = editor.getSelectionEnd();
          editor.setCaretPosition(pos);
          a = editor.getCharacterAttributes();
          editor.select(pos, end);

        } else {
          int pos = editor.getCaretPosition();
          editor.setCaretPosition(
              (pos == 0 ? pos : pos -1) );
          a = editor.getCharacterAttributes();
          editor.setCaretPosition(pos);
        }

        MutableAttributeSet s = new SimpleAttributeSet(a);
        return s;
    }

    public boolean save(File f)
    {
      if(f == null) return false;
      try {
        RTFEditorKit rtf = new RTFEditorKit();
        FileOutputStream fos = new FileOutputStream(f);
        rtf.write(fos, editor.getStyledDocument(), 0,
            editor.getDocument().getEndPosition().getOffset());
        fos.flush();
        fos.close();
        return true;
      } catch(Exception ex) {
        return false;
      }
    }


  // Variables declaration - do not modify//GEN-BEGIN:variables
  private com.power.visual.BlackButton boldButton;
  private javax.swing.JPanel buttonsPanel;
  private com.power.visual.BlackButton centerButton;
  private javax.swing.JTextPane editor;
  private com.power.visual.BlackButton fontButton;
  private com.power.visual.BlackButton italicButton;
  private javax.swing.JScrollPane jScrollPane1;
  private com.power.visual.BlackButton justifyButton;
  private com.power.visual.BlackButton leftButton;
  private com.power.visual.BlackButton rightButton;
  private com.power.visual.BlackButton underlineButton;
  // End of variables declaration//GEN-END:variables


  public static void main(String[] args)
  {
    JFrame f = new JFrame("TextEditorPanel");
    f.add(new TextEditorPanel());
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }

}
